package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dl7.recycler.divider.VerticalDividerItemDecoration;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdStyleBean;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.BaseAdListener;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.MaleOrFemaleWeekAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 男/女频本周热搜
 * Created by wcy on 18/6/4.
 */
public class AdSelectedFour3Layout extends LinearLayout implements IRefresh<SelectedBean> {
    @BindView(R.id.label)
    TextView mLabel;
    @BindView(R.id.container)
    RecyclerView mContainer;
    Unbinder mUnbinder;
    ItemRefreshBean mItemRefreshBean;
    @BindView(R.id.sub_title)
    TextView mSubTitle;
    @BindView(R.id.more)
    TextView mMore;
    RecyclerView.ItemDecoration mItemDecoration;
    private RecyclerView.Adapter mAdapter;
    private String mType;
    public AdSelectedFour3Layout(Context context) {
        super(context);
        init();
    }

    public AdSelectedFour3Layout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedFour3Layout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_four_layout, this);
        mUnbinder = ButterKnife.bind(view);
    }

    @Override
    public void refresh(SelectedBean selectedBean, Object object) {
        if (selectedBean == null || object == null) {
            return;
        }
        mItemRefreshBean = (ItemRefreshBean) object;
        mLabel.setText(mItemRefreshBean.selectionName);
        mLabel.setCompoundDrawablesWithIntrinsicBounds(mItemRefreshBean.selectionIcon, 0, 0, 0);
        if (TextUtils.isEmpty(mItemRefreshBean.subTitle)) {
            DisplayUtils.gone(mSubTitle);
        } else {
            DisplayUtils.visible(mSubTitle);
            mSubTitle.setText(mItemRefreshBean.subTitle);
        }
        inVisibleMoreEntry(mItemRefreshBean.type);
        String type = mItemRefreshBean.type;
        mType = type;
        if (mAdapter == null) {
            mLabel.setCompoundDrawablesWithIntrinsicBounds(mItemRefreshBean.selectionIcon, 0, 0, 0);
            if (mItemDecoration != null) {
                mContainer.removeItemDecoration(mItemDecoration);
            }
            mItemDecoration = new VerticalDividerItemDecoration.Builder(getContext()).size(DisplayUtils.dip2px(getContext(), 10)).color(getResources().getColor(R.color.transparent)).build();
            mContainer.addItemDecoration(mItemDecoration);
            mAdapter = new MaleOrFemaleWeekAdapter(getContext(), selectedBean.list, mItemRefreshBean.tabType);
            int size = selectedBean.list.size();
            int adPositionId;
            if (size > 1) {
                if (TextUtils.equals(Constants.BookCityModuleType.TYPE_MALE_BENZHOURESOU, mType)) {
                    adPositionId = ADConfigBean.POSITION_MALE_BENZHOURESOU_MP_1;
                } else {
                    adPositionId = ADConfigBean.POSITION_FEMALE_BENZHOURESOU_NP_1;
                }
                refreshAd(2, selectedBean, adPositionId);
            } else {
                mArray.put(2, true);
            }
            if (size > 3) {
                if (TextUtils.equals(Constants.BookCityModuleType.TYPE_MALE_BENZHOURESOU, mType)) {
                    adPositionId = ADConfigBean.POSITION_MALE_BENZHOURESOU_MP_2;
                } else {
                    adPositionId = ADConfigBean.POSITION_FEMALE_BENZHOURESOU_NP_2;
                }
                refreshAd(4, selectedBean, adPositionId);
            } else {
                mArray.put(4, true);
                if(mArray.get(2) && mArray.get(4)) {
                    RecyclerViewHelper.initRecyclerViewH(getContext(), mContainer, mAdapter);
                }
            }
        }

    }

    @Override
    public void onError(int code, String desc) {

    }

    @Override
    public void reset() {
        if (mArray != null) {
            mArray.clear();
        }
        mAdapter = null;
    }

    @OnClick(R.id.more)
    public void onViewClicked() {
    }

    private void inVisibleMoreEntry(String type) {
        if (Constants.BookCityModuleType.TYPE_MALE_BENZHOURESOU.equals(type)
                || Constants.BookCityModuleType.TYPE_FEMALE_BENZHOURESOU.equals(type)) {
            DisplayUtils.gone(mMore);
        } else {
            DisplayUtils.visible(mMore);
        }
    }

    private SparseBooleanArray mArray = new SparseBooleanArray();

    public void refreshAd(int pos, SelectedBean selectedBean, int adPosition) {
        AdUtils.Companion.fetchAdUtil((Activity) getContext(), null, null, adPosition, AdStyleBean.BOOK_COVER_STYLE, R.layout.item_male_or_female_week_layout, new BaseAdListener() {
            @Override
            public void onAdLoadSucceeded(@org.jetbrains.annotations.Nullable View view) {
                BookInfo bookInfo = new BookInfo();
                bookInfo.adview = view;
                int index = 0;
                mArray.put(pos, true);
                if (pos == 2) {
                    index = pos;
                }
                if (pos == 4) {
                    index = pos;
                    if (mArray.get(2)) {
                        index++;
                    }
                }
                if (index <= selectedBean.list.size()) {
                    if (index < selectedBean.list.size()) {
                        BookInfo bookInfo1 = selectedBean.list.get(index);
                        if (bookInfo1 != null && bookInfo1.adview != null) {
                            selectedBean.list.remove(index);
                        }
                    }
                    selectedBean.list.add(index, bookInfo);
                }
                if(mArray.get(2) && mArray.get(4)) {
                    RecyclerViewHelper.initRecyclerViewH(getContext(), mContainer, mAdapter);
                }
            }

            @Override
            public void onAdLoadFailed() {
                mArray.put(pos, true);
                if(mArray.get(2) && mArray.get(4)) {
                    RecyclerViewHelper.initRecyclerViewH(getContext(), mContainer, mAdapter);
                }
            }

            @Override
            public void onAdClicked() {

            }

            @Override
            public void onADClosed() {

            }

            @Override
            public void onBackAd(@NotNull Object ad) {

            }
        });
    }
}
